// pages/orderDetail/orderDetail.js
const api = require('../../api/index'); // 头部引入
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderStatus: "已完成",
    orderDetail: {},
    statusList: ["", "待付款", "待接单", "已接单", "派送中", "已完成", "已取消", "退款"],
    orderTypeList:["堂食", "店内自取", "配送到家"],
    tipTextList: ["", "订单还未待付款~~", "订单正在等待商家接单~~", "商家接单了~~", "商家正在快马加鞭派送中~~", "感谢信任，期待再次光临~~", "订单取消了~~"],
    orderId: "",
    showCancelOrder: false,
    isShowRefund:false
  },
  //取消订单弹出层
  onCancel() {
    this.setData({
      showCancelOrder: true
    })
  },
  //确认取消订单
  onConfirmOrder() {
    let id = this.data.orderDetail.id
    api.orderCancel(id).then(res=>{
      if(res.code == 1){
        this.setData({
          showCancelOrder: false
        })
        wx.redirectTo({
          url: '/pages/orderDetail/orderDetail?orderId=' + this.data.orderId,
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon:'error'
        })
      }
    })

  },
  //取消订单弹出层取消
  onCancelOrder() {
    this.setData({
      showCancelOrder: false
    })
  },
  //申请退款
  onRefund() {
    wx.showToast({
      title: '请联系商家进行退款！',
      icon:'none'
    })
  },
  //再来一单
  onOrder() {
    api.orderRepetition(this.data.orderId).then(res => {
      if (res.code == 1) {
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }
    })
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //联系商家
  onContact() {
    wx.makePhoneCall({
      phoneNumber: '6666666666',
      /*模拟的电话号码*/
    })
  },
  //获取订单详情信息
  orderDetail() {
    api.orderDetail(this.data.orderId).then(res => {
      console.log(res);
      if (res.code == 1) {
        this.setData({
          orderDetail: res.data
        })
      }
    })
  },
  //判断订单是否超过三天
  isThreeDaysApart(){
    let date1 = new Date(this.data.orderDetail.orderTime);
    let date2 = new Date();
    let timeDiff = Math.abs(date1.getTime() - date2.getTime());
    let diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
    if (diffDays === 3) {
      console.log('两个日期相差3天');
      this.setData({
        isShowRefund:false,
      })
    } else {
      console.log('两个日期不相差3天');
      this.setData({
        isShowRefund:true,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      orderId: options.orderId
    })
    this.orderDetail();

    this.isThreeDaysApart()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})